package starter.pageobjects.orangehrmlive;

import net.serenitybdd.core.pages.PageObject;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.time.Duration;

/**
 * @ClassName MenuPage
 * @Date 2024/11/2 12:07 47
 * @Author Jack Chen
 * @Description serenity-cucumber-auto-project-jack
 * @Version 1.0
 */

public class NavbarPage extends PageObject {

    public static final By SearchBox = By.cssSelector("input[placeholder='Search']");
    public static final By Admin_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Admin']");
    public static final By PIM_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='PIM']");
    public static final By Leave_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Leave']");
    public static final By Time_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Time']");
    public static final By Recruitment_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Recruitment']");
    public static final By MyInfo_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='My Info']");
    public static final By Performance_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Performance']");
    public static final By Dashboard_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Dashboard']");
    public static final By Directory_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Directory']");
    public static final By Maintenance_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Maintenance']");
    public static final By Claim_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Claim']");
    public static final By Buzz_Menu = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]/span[text()='Buzz']");
    public static final By MenuInfoShowOrHide_Button = By.cssSelector("input+button.oxd-main-menu-button");
    public static final By CurrentDisplayedMenus = By.xpath("//ul[@class='oxd-main-menu']//a[contains(@class,'oxd-main-menu-item')]");

    public void inputSearchBox(String text) {
//        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(10));
//        WebElement searchBox = wait.until(ExpectedConditions.visibilityOfElementLocated(MenuPage.SearchBox));
        $(SearchBox).type(text);
    }
    public void clickOnAdminMenu() {
        $(Admin_Menu).click();
    }
    public void clickOnPIMMenu() {
        $(PIM_Menu).click();
    }
    public void clickOnLeaveMenu() {
        $(Leave_Menu).click();
    }
    public void clickOnTimeMenu() {
        $(Time_Menu).click();
    }
    public void clickOnRecruitmentMenu() {
        $(Recruitment_Menu).click();
    }
    public void clickOnMyInfoMenu() {
        $(MyInfo_Menu).click();
    }
    public void clickOnDashboardMenu() {
        $(Dashboard_Menu).click();
    }
    public void clickOnDirectoryMenu() {
        $(Directory_Menu).click();
    }
    public void clickOnMaintenanceMenu() {
        $(Maintenance_Menu).click();
    }
    public void clickOnClaimMenu() {
        $(Claim_Menu).click();
    }
    public void clickOnBuzzMenu() {
        $(Buzz_Menu).click();
    }
    public void clickOnMenuInfoShowOrHideMenu() {
        $(MenuInfoShowOrHide_Button).click();
    }

    public boolean checkSearchBoxShown() {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(10));
        WebElement searchBox = wait.until(ExpectedConditions.visibilityOfElementLocated(NavbarPage.SearchBox));
        return searchBox.isDisplayed();
    }

    private By getMenuBy(String menuName) {
        if (!menuName.isBlank()) {
            return switch (menuName) {
                case "Admin" -> Admin_Menu;
                case "PIM" -> PIM_Menu;
                case "Leave" -> Leave_Menu;
                case "Time" -> Time_Menu;
                case "Recruitment" -> Recruitment_Menu;
                case "My Info" -> MyInfo_Menu;
                case "Performance" -> Performance_Menu;
                case "Dashboard" -> Dashboard_Menu;
                case "Directory" -> Directory_Menu;
                case "Maintenance" -> Maintenance_Menu;
                case "Claim" -> Claim_Menu;
                case "Buzz" -> Buzz_Menu;
                default -> null;
            };
        } else {
            System.out.println("Menu not found");
            return null;
        }
    }

    public boolean checkSearchMenuDisplayed(String menuName) {
        By by = getMenuBy(menuName);
        if (by != null) {
            Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(10));
            WebElement menu = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
            return menu.isDisplayed();
        } else {
            return isMenuListEmpty();
        }
    }

    private boolean isMenuListEmpty() {
        try {
            $(NavbarPage.CurrentDisplayedMenus).isDisplayed();
            return false;
        } catch (NoSuchElementException e) {
            return true;
        }
    }

    public boolean checkMenuDisplayed(String menuName) {
        By by = getMenuBy(menuName);
        if (by != null) {
            Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(10));
            WebElement menu = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
            return menu.isDisplayed();
        } else {
            return isMenuListEmpty();
        }
    }

    public void clickOnMenu(String menuName) {
        Wait<WebDriver> wait = new WebDriverWait(getDriver(), Duration.ofSeconds(10));
        WebElement menu = wait.until(ExpectedConditions.elementToBeClickable(getMenuBy(menuName)));
        clickOn(menu);
    }


}
